package dbz.controllers;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import dbz.domain.Card;
import dbz.domain.Deck;
import dbz.domain.Fighter;
import dbz.domain.Health;
import dbz.domain.Ki;
import dbz.domain.Match;
import dbz.domain.attack.Attack;
import dbz.domain.attack.conditions.AttackCondition;
import dbz.domain.attack.conditions.KiLevelCondition;
import dbz.domain.dtos.HandDTO;
import dbz.domain.fighter.status.RegularStatus;
import dbz.domain.player.Player;

@Controller
public class HomeController {

	
	@RequestMapping("/createMatch.do")
	protected ModelAndView doCreateMatch() {
		AttackCondition condition = new KiLevelCondition(1);
		List<AttackCondition> conditions = new ArrayList<AttackCondition>();
		conditions.add(condition);

		AttackCondition condition2 = new KiLevelCondition(1);
		List<AttackCondition> conditions2 = new ArrayList<AttackCondition>();
		conditions2.add(condition2);
		
		Attack kamehameha = new Attack("kame-hame-ha", 20, conditions);
		Attack galitGun = new Attack("gallit gun", 20, conditions2);
		
		List<Attack> gokuAttacks = new ArrayList<Attack>();
		List<Attack> vegetaAttacks = new ArrayList<Attack>();
		gokuAttacks.add(kamehameha);
		vegetaAttacks.add(galitGun);
		
		Fighter goku = new Fighter("Goku", new Health(120),new RegularStatus(), gokuAttacks);
		Fighter vegeta = new Fighter("Vegeta", new Health(120),new RegularStatus(), vegetaAttacks);
		
		List<Card> oneCards = new ArrayList<Card>();
		List<Card> twoCards = new ArrayList<Card>();
		oneCards.add(goku);
		oneCards.add(new Ki());
		oneCards.add(new Ki());
		oneCards.add(new Ki());
		oneCards.add(new Ki());
		
		twoCards.add(vegeta);
		twoCards.add(new Ki());
		twoCards.add(new Ki());
		twoCards.add(new Ki());
		twoCards.add(new Ki());
		
		Deck playerOnesDeck = new Deck(oneCards);
		Deck playerTwosDeck = new Deck(twoCards);
		
		Player player1 = new Player(playerOnesDeck);
		Player player2 = new Player(playerTwosDeck);
		
		Match match = Match.getInstance(player1, player2);
		
		ModelAndView modelAndView = new ModelAndView("match");
		return modelAndView;
		
	}
	
	@RequestMapping("/obtainMatch.do")
	public @ResponseBody String doObtainMatch() {
		HandDTO handDTO = new HandDTO(Match.getInstance().getPlayer1().getHand());
		ObjectMapper mapper = new ObjectMapper();
		StringWriter stringWriter = new StringWriter();
		if (mapper.canSerialize(HandDTO.class)) {
			try {
				mapper.writeValue(stringWriter, handDTO);
			} catch (JsonGenerationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return stringWriter.getBuffer().toString();
	}
}
